﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Data.Model;
using ZLCMS.Business.TranferParam;
using ZLCMS.Web.SystemManage.Control;
using System.Collections.Generic;
using Newtonsoft.Json;
using NHibernate;
using DotNet.Common.TransferParam;
using NHibernate.Criterion;
using ZLCMS.Business.Shop.Data;

namespace ZLCMS.Web.SystemManage.Basics
{
    public partial class MealAdd : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        public string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                AddBussines();
            }
        }

        protected void InitializeGlobalValue()
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);
            _userAccount = UserKit.GetUserAccountFailLogin();
        }

        #region 初始化内容 -protected void InitializeContent()
        /// <summary>
        /// 初始化内容
        /// </summary>
        protected void InitializeContent()
        {
            fc_open.Config = new Business.UploadFileInfo.UploadConfig()
            {
                Thumbnail = new ThumbnailConfig()
                {
                    Mode = "HW",
                    Auto = true,
                    Name = ".thumb.jpg",
                    Height = 100,
                    Width = 100
                },
                UploadDir = _userAccount + "/product/"
            };
            ////页面控件值
            if (!IsPostBack)
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                }
            }
        }
        #endregion

        #region 添加产品 +protected void AddBussines()
        /// <summary>
        /// 添加产品
        /// </summary>
        protected void AddBussines()
        {
            ShopProductInfo productDB = new ShopProductInfo();
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                if (string.IsNullOrEmpty(tb_name.Text) || string.IsNullOrEmpty(tb_productno.Text))
                {
                    _framePage.Alert(new ReturnCode()
                    {
                        statusCode = 300,
                        message = "参数错误!",
                        callbackType = "closeCurrent",
                        navTabId = _framePage.NavTabId
                    });
                }
                else
                {
                    int _count = Convert.ToInt32(session.CreateSQLQuery("select count(*) from SHOP_PRODUCT_INFO where product_no='" + tb_productno.Text + "' and OWNER_ACCOUNT='" + _userAccount + "' and kind='restaurant'").UniqueResult());
                    if(_count>0)
                    {
                         _framePage.Alert(new ReturnCode()
                    {
                        statusCode = 300,
                        message = "该产品编号已存在!",
                        callbackType = "closeCurrent",
                        navTabId = _framePage.NavTabId
                    });
                    }
                    else
                    {
                     productDB.ProductName = tb_name.Text;
                    productDB.ProductNo = tb_productno.Text;
                    //productDB.ProductNo = tb_brand.Text;

                    productDB.TypeId = ddl_producttype.SelectedNodeId;
                    productDB.CustomField = GetCustomString();

                    productDB.ShortDesc = tb_shortdesc.Text;
                    productDB.SalePrice = decimal.Parse(tb_sprice.Text);

                    productDB.Score = 0;
                    productDB.Coins = 0;
                    // productDB.MiniNum = int.Parse(tb_min.Text);

                    productDB.ThumbnailImg = fc_open.Value;
                    productDB.IsHot = "0";
                    productDB.IsSpecialOffer = "0";
                    productDB.IsVirtual = "0";
                    productDB.IsForSale = "0";

                    if (cbxCommend.Checked == true)
                    {
                        productDB.IsCommend = "1";
                    }
                    else
                    {
                        productDB.IsCommend = "0";
                    }

                    productDB.AddTime = DateTime.Now;
                    productDB.Status = "0";
                    productDB.Hits = 0;
                    productDB.Kind = "restaurant";

                    int id = productDB.Id;

                    productDB.Description = txt_description.Value;
                    // productDB.PackContent = txt_bzqd.Value;
                    // productDB.ParamContent = txt_spcs.Value;
                    // productDB.GiftContent = txt_zengs.Value;
                    productDB.OwnerAccount = _userAccount;

                    session.Save(productDB);
                    session.Flush();

                    _framePage.Alert(new ReturnCode()
                    {
                        statusCode = 200,
                        message = "操作成功!",
                        callbackType = "closeCurrent",
                        navTabId = _framePage.NavTabId
                    });
                    }
                }
            }
        }
        #endregion

        #region 获取定制属性内容 -protected string GetCustomString()
        /// <summary>
        /// 获取定制属性内容
        /// </summary>
        /// <returns></returns>
        protected string GetCustomString()
        {
            Label lbId;
            CheckBoxControl chkblist;

            List<ProductCustomData> dataList = new List<ProductCustomData>();

            //foreach (RepeaterItem item in rp_pjlb.Items)
            //{
            //    lbId = (Label)item.FindControl("lb_pjid");
            //    chkblist = (CheckBoxControl)item.FindControl("cbc_list");

            //    if (!string.IsNullOrEmpty(chkblist.SelectedValue))
            //    {
            //        dataList.Add(new ProductCustomData() { FieldKind = int.Parse(lbId.Text), FieldSelected = chkblist.SelectedValue });
            //    }
            //}

            return JsonConvert.SerializeObject(dataList);
        }
        #endregion
    }
}
